package main

import (
	"bufio"
	"flag"
	"fmt"
	"gitee.com/goxiang2/server/lib/lib_watch"
	"io"
	"os"
	"strings"
)

var host = flag.String("h", "", "master address")
var file = flag.String("f", "", "paramter file name")

func ReadLine(fileName string) ([]string, error) {
	f, err := os.Open(fileName)
	if err != nil {
		return nil, err
	}
	buf := bufio.NewReader(f)
	var ret []string
	for {
		line, err := buf.ReadString('\n')
		line = strings.TrimSpace(line)
		ret = append(ret, line)
		if err != nil {
			if err == io.EOF {
				return ret, nil
			}
			return nil, err
		}
	}
	return ret, nil
}

func main() {
	flag.Parse()

	if *file == "" || *host == "" {
		flag.Usage()
		return
	}

	client, err := lib_watch.NewWatchClient(*host, []string{})
	if err != nil {
		fmt.Println(err)
		return
	}

	client.Start()

	param, err1 := ReadLine(*file)
	if err1 != nil {
		fmt.Println(err1)
		return
	}

	ret := client.RawCall(param)

	for i := 0; i < len(ret); i++ {
		fmt.Println(ret[i])
	}

	client.Shutdown()
}
